package com.csi.service.impl;

import com.csi.domain.Employee;
import com.csi.domain.Role;
import com.csi.domain.vo.EmployeeDetail;
import com.csi.mapper.EmployeeMapper;
import com.csi.service.EmployeeService;
import com.csi.utils.MD5Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
 public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     * @param username 用户名
     * @param password 密码
     * @param hotelId 酒店id
     * @return 执行结果
     */
    @Override
    public Employee login(String username, String password,long hotelId) {

        return employeeMapper.login(username,password,hotelId);
    }

    /**
     * 保存员工信息
     * @param employee 员工对象
     * @return 保存成功的记录数
     */
    @Override
    public int save(Employee employee) {
        employee.setPassword(MD5Utils.getMD5String(employee.getPassword()));
        return employeeMapper.save(employee);
    }

    /**
     * 更新员工信息
     * @param employee 员工对象
     * @return 更新成功的记录数
     */
    @Override
    public int update(Employee employee) {
        employee.setPassword(MD5Utils.getMD5String(employee.getPassword()));
        return employeeMapper.update(employee);
    }

    /**
     * 删除员工信息
     * @param employee 员工对象
     * @return 执行结果
     */
    @Override
    public int delete(Employee employee) {
        return employeeMapper.delete(employee);
    }

    /**
     * 修改权限（角色）
     * @param employee
     * @return
     */
    @Override
    public int modifyRole(Employee employee) {
        return employeeMapper.modifyRole(employee);
    }

    /**
     * 根据id查找用户
     * @param id
     * @return
     */
    @Override
    public EmployeeDetail findById(long id) {
        return employeeMapper.findById(id);
    }


    /**
     * 模糊查询用户信息
     * @param name
     * @param roleId
     * @param sex
     * @return
     */
    @Override
    public PageInfo<EmployeeDetail> findLike(String name, long roleId, int sex, long hotelId, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        // 执行查询
        List<EmployeeDetail> employeeList = employeeMapper.findLike(name, roleId, sex, hotelId);

        // 使用 PageInfo 来包装查询结果，它会自动包含分页信息（如总记录数、当前页码等）
        return new PageInfo<>(employeeList);
    }

    /**
     * 根据员工名字和酒店id查找员工
     * @param username
     * @param hotelId
     * @return
     */
    @Override
    public EmployeeDetail findByName(String username, long hotelId) {
        return employeeMapper.findByName(username,hotelId);
    }


    /**
     * 根据角色id查找员工
     * @param roleId
     * @return
     */
    @Override
    public List<Employee> findByRole(long roleId) {
        return employeeMapper.findByRole(roleId);
    }

    /**
     * 查看已离职员工
     * @param hotelId
     * @return
     */
    @Override
    public List<EmployeeDetail> findLeaveEmployee(long hotelId) {
        return employeeMapper.findLeaveEmployee(hotelId);
    }

    /**
     * 根据员工id查找员工名
     * @param employeeId
     * @return
     */
    @Override
    public String findUserName(long employeeId) {
        return employeeMapper.findUserName(employeeId);
    }

    @Override
    public int updatePassword(String oldLoginPassword, String newLoginPassword, long employeeId) {
        return employeeMapper.updatePassword(oldLoginPassword,newLoginPassword,employeeId);
    }

}
